//
//  SettingViewController.m
//  quickDoctor
//
//  Created by zhaowei on 17/3/9.
//  Copyright © 2017年 SWS. All rights reserved.
//

#import "SettingViewController.h"
#import "IdeaBackViewController.h"
#import <MBProgressHUD.h>

@interface SettingViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSArray *arr;

@end
static NSString *identifier = @"hh";
@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
}

- (void)setupUI
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    _tableView = tableView;
    tableView.dataSource = self;
    tableView.delegate = self;
    
    [self.view addSubview:tableView];
    
    tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.1)];
    
    //注册cell
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    //配置
    self.arr = @[@"意见反馈",@"加号符号说明",@"修改密码",@"注销用户"];
    
}

#pragma mark
#pragma mark -数据源
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    if (!cell) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text = self.arr[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSInteger row = indexPath.row;
    switch (row) {
        case 0:
        {
            IdeaBackViewController *IBController = [[IdeaBackViewController alloc] init];
            [self.navigationController pushViewController:IBController animated:NO];
            break;
        }
        case 3:
        {
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"您确定要注销吗?" preferredStyle:UIAlertControllerStyleActionSheet];
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
            UIAlertAction *ensureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
                NSUserDefaults   *userDefaults = [NSUserDefaults standardUserDefaults];
                NSString *password = [userDefaults objectForKey:@"password"];
                if(password != nil) {
                //注销成功弹框
                MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
                hud.mode = MBProgressHUDModeCustomView;
                UIImage *image = [[UIImage imageNamed:@"Checkmark"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
                hud.customView = [[UIImageView alloc] initWithImage:image];
                hud.square = YES;
                hud.label.text = NSLocalizedString(@"注销成功", @"HUD done title");
                hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
                hud.backgroundView.color = [UIColor colorWithWhite:0.1f alpha:0.1f];
                [hud hideAnimated:YES afterDelay:1.f];
                
                //移除userDefaults里面的用户信息
                NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
//                [userDefaults removeObjectForKey:@"username"];
                [userDefaults removeObjectForKey:@"password"];
                [userDefaults synchronize];
                }else {
                     UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"您还没有登录!" preferredStyle:UIAlertControllerStyleAlert];
                    UIAlertAction *ensure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
                    [alertController addAction:ensure];
                    [self presentViewController:alertController animated:YES completion:^{
                        
                    }];
                }
                
            }];
            [alertController addAction:cancelAction];
            [alertController addAction:ensureAction];
            [self presentViewController:alertController animated:YES completion:nil];
            break;
        }
        default:
            break;
    }
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
